package com.guixiansong.rabbitmq;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

public class Before {
    //创建连接rabbitmq的连接工厂对象
    ConnectionFactory connectionFactory = null;
    //获取连接对象
    Connection connection = null;
    Channel channel = null;
    public void before() throws IOException, TimeoutException {
        //创建连接rabbitmq的连接工厂对象
//        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory=new ConnectionFactory();
        //设置连接rabbitmq的主机
        connectionFactory.setHost("localhost");
        //设置连接rabbitmq的端口号
        connectionFactory.setPort(5672);
        //设置连接哪个虚拟主机
        connectionFactory.setVirtualHost("gxs");
        //设置连接该虚拟主机的用户名和密码
        connectionFactory.setUsername("gxs");
        connectionFactory.setPassword("gxs");

        //获取连接对象
//        Connection connection = connectionFactory.newConnection();
        connection=connectionFactory.newConnection();
        //获取连接中通道
        channel = connection.createChannel();

        //通道绑定对应消息队列
        //参数一：队列名称，如果不存在则自动创建
        //参数二：用来定义队列特性是否要持久化  true：持久化队列    false：不持久化队列
        //参数三: exclusive：是否独占队列  true：独占队列   false：不独占队列
        //参数四：autoDelete：是否在消费完成后自动删除队列  true：自动删除   false：不自动删除
        //参数五：额外附加参数
        channel.queueDeclare("hello",false,false,false,null);
    }

    public void close() throws IOException, TimeoutException {
        this.channel.close();
        this.connection.close();
    }

    public Before() throws IOException, TimeoutException {
        this.before();
    }
}
